export const getCustomBar = () => {
  const { platform } = uni.getSystemInfoSync();
  if (platform == "android") {
    return 48;
  } else {
    return 44;
  }
};

export function oversize() {
  uni.$u.toast("超出最大限制，最大为8M");
}

export const uploadSize = 8 * 1024 * 1024;

/**
 * 金额格式
 */
export const isPrice = (value) => {
  return /^\d+(\.\d{1,2})?$/.test(value);
};

export const IOSDate = (dateTime) => {
  if (!dateTime) return dateTime;
  return dateTime
    .replace(/-/g, "/")
    .replace(/.000/g, "")
    .replace(/T/g, " ")
    .replace(/Z/g, " ");
};

// /**
//  * @description 统计转化
//  * 栗:9998 => 9000+
//  */
// export const totalTransform = (num) => {
//   if (isNaN(num)) return num;
//   num = num.toString();
//   const first = num[0];
//   const len = num.length;
//   if (len <= 4) {
//     return num;
//   }
//   // 过万
//   if (len >= 5) {
//     return first.padEnd(len > 6 ? 2 : len - 4, "0");
//   }
// };

/**
 * @description 统计转化
 * 栗:9998 => 9000+
 */
export const totalTransform = (num) => {
  if (isNaN(num)) return num;
  num = num.toString();
  const first = num[0];
  const len = num.length;
  if (len <= 3) {
    return num;
  }
  // 过千
  if (len >= 4) {
    return first.padEnd(len > 5 ? 2 : len - 3, "0");
  }
};

/**
 * @description 获取元素尺寸
 * @returns bottom: Number,
			dataset: {ref: ""},
			height: Number,
			id: String,
			left: Number,
			right: Number,
			top: Number,
			width: Number ,
 */
export const GetRect = (selector, app) => {
  return new Promise((resolve) => {
    uni
      .createSelectorQuery()
      .in(app)
      ["select"](selector)
      .boundingClientRect((rect) => {
        resolve(rect);
      })
      .exec();
  });
};

/**
 * 多个经纬度计算中心点
 */
export const getPointsCalculateCenter = (points = []) => {
  let point_num = points.length; //坐标点个数
  let X = 0,
    Y = 0,
    Z = 0;
  for (let i = 0; i < points.length; i++) {
    if (points[i] == "") {
      continue;
    }
    let point = points[i];
    let lat, lng, x, y, z;
    lat = (parseFloat(point[0]) * Math.PI) / 180;
    lng = (parseFloat(point[1]) * Math.PI) / 180;
    x = Math.cos(lat) * Math.cos(lng);
    y = Math.cos(lat) * Math.sin(lng);
    z = Math.sin(lat);
    X += x;
    Y += y;
    Z += z;
  }
  X = X / point_num;
  Y = Y / point_num;
  Z = Z / point_num;
  let tmp_lng = Math.atan2(Y, X);
  let tmp_lat = Math.atan2(Z, Math.sqrt(X * X + Y * Y));
  return [(tmp_lat * 180) / Math.PI, (tmp_lng * 180) / Math.PI];
};

export const removeHtmlTags = (text) => {
  return text.replace(/<[^>]+>/g, "");
};

/**
 * 手机号脱敏显示
 */
export const hidePhone = (tel) => {
  if (!tel) return;
  let pat = /(\d{3})\d*(\d{4})/;
  let hideTel = tel.replace(pat, "$1****$2");
  return hideTel;
};
